var sample = {};

sample.Application = function(){};

core.extendClass('sample.Application', 'core.JSApplication');

sample.Application.prototype.loadDockIndicator = function(aWorkspace){
    core.log('fill loading indicator here', aWorkspace, aWorkspace.dockElement);
    var d = core.newDiv(null, 'dock_icon');
    $(d).html('Y');
    d.side = 25;
    aWorkspace.dockElement.addElement(d);
    aWorkspace.dockElement.menu = [{
            text: 'Menu item, close for example',
            handler: function(){
                return true;
            }
    }]
};

sample.MainFrame = function(){};

core.extendClass('sample.MainFrame', 'core.JSFrame');

sample.MainFrame.prototype.editEntry = function(obj){
//    core.log('ready to edit: '+obj.id, obj.lastName);
//    this.lastNameEd.setValue(obj.lastName);
//    this.firstNameEd.setValue(obj.firstName);
//    this.emailEd.setValue(obj.email);
//    this.notesEd.setValue(obj.notes);
    this.frame.body.setValue(obj);
//    this.frame.body.updateLayout();
//    this.frame.body.refresh(true);
//    this.lastNameEd.doFocus();
}

sample.MainFrame.prototype.loadContents = function(){
    var instance = this;
    this.toolbar = new core.Tabs({
        toolbar: true
    });
    this.newButton = new core.form.Button({
        text: 'New entry',
        toolbarStyle: true,
        handler: function(){
            instance.sendMessage('newEntry', {}, null, null, instance.frame.body);
        }
    });

    this.saveButton = new core.form.Button({
        text: 'Save',
        toolbarStyle: true,
        handler: function(){
            instance.send({
                method: 'saveEntry',
                message: instance.frame.body.getValue(),
                disable: instance.frame.body
            });
        }
    });

    this.deleteButton = new core.form.Button({
        text: 'Remove',
        toolbarStyle: true,
        handler: function(){
            instance.send({
                method: 'removeEntry',
                disable: instance.frame.body
            });
        }
    });


    this.lastNameEd = new core.form.TextField({
        id: 'lastName'
    });
    this.firstNameEd = new core.form.TextField({
        id: 'firstName'
    });
    this.emailEd = new core.form.TextField({
        id: 'email'
    });
    this.workEmailEd = new core.form.TextField({
        id: 'work_email'
    });
    this.workPhoneEd = new core.form.TextField({
        id: 'work_phone'
    });
    this.homePhoneEd = new core.form.TextField({
        id: 'home_phone'
    });
    this.cellPhoneEd = new core.form.TextField({
        id: 'cell_phone'
    });
    this.birthdayEd = new core.form.TextField({
        id: 'birthday'
    });
    this.notesEd = new core.form.TextArea({
        id: 'notes',
        colspan: 3
    });
    this.frame.addToolbar(this.toolbar);
    this.toolbar.addTab(this.newButton, this.saveButton, this.deleteButton);
    this.setContent([
        core.createLabel('First name:'), this.firstNameEd,
        core.createLabel('Last name:'), this.lastNameEd,
        core.createLabel('Work e-mail:'), this.workEmailEd,
        core.createLabel('Home e-mail:'), this.emailEd,
        core.createLabel('Work phone:'), this.workPhoneEd,
        core.createLabel('Home phone:'), this.homePhoneEd,
        core.createLabel('Cell phone:'), this.cellPhoneEd,
        core.createLabel('Birthday:'), this.birthdayEd,
        core.createLabel('Notes:'), this.notesEd
    ], '0.15, 0.35, 0.15, 0.35', '4xd, 1');
}

sample.TreeFrame = function(){};

core.extendClass('sample.TreeFrame', 'core.JSFrame');

sample.TreeFrame.prototype.reloadTree = function(data){
    var instance = this;
    this.tree.root.clear();
    if(data && data.data){
        for (var i = 0; i < data.data.length; i++) {
            var item = data.data[i];
            var ch = instance.tree.root.addChild(new core.tree.TreeItem({
                text: item.name,
                type: 1,
                icon_cls: item.icon,
                id: item.id
            }));
            if(data.selected==item.id){
                instance.tree.selectElement(ch);
            }
        }
    }
    this.tree.bodyChanged();
}

sample.TreeFrame.prototype.updateTree = function(){
    var instance = this;
    this.sendMessage('getEntries', {}, function(data){
        instance.reloadTree(data);
    }, null, this.tree);
}

sample.TreeFrame.prototype.loadContents = function(){
    var instance = this;
    this.refreshBtn = new core.form.Button({
        text: 'Reload',
        toolbarStyle: true,
        handler: function(){
            instance.updateTree();
        }
    });

    this.dlg = new core.form.Button({
        text: 'Show dlg',
        toolbarStyle: true,
        handler: function(){
            instance.sendMessage('openDlg', {}, null, null, this);
        }
    });

    this.toolbar = new core.Tabs({
        toolbar: true,
        cls: 'toolbar'
    });
    this.toolbar.addTab(this.refreshBtn);
    this.frame.addToolbar(this.toolbar);
    
    this.tree = new core.tree.TreePanel({
        
    });
    this.tree.on('select', function(i, e, el){
        instance.send({
            method: 'selectEntry',
            message: {
                id: el?el.id:null
            },
            disable: instance.application.getFrameElement('main-frame') || instance.tree
        });
    });
    this.setContent([this.tree], '1', '1');
    this.updateTree();
};

sample.FloatFrame = function(){};

core.extendClass('sample.FloatFrame', 'core.JSFrame');

sample.dlg1 = function(){};

core.extendClass('sample.dlg1', 'core.JSDialog');

sample.dlg1.prototype.createDialog = function(aData){
    var instance = this;
    return core.createDialog(aData.message, 'Message:', 300, function(){
        instance.sendMessage('closeDialog', {}, null, null, instance.dialog);
    });
};

